package com.travel.server.manager.impl.datasource.util;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/16 14:52
 * @Desc: druid连接池工具类
 */
public class DruidUtil {

    public static DataSource createXADataSource(String driverClassName, String uniqueResourceName, String url, String userName, String password,
                                                int initialSize, int minIdle, int maxActive, int maxWait) throws SQLException {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName(uniqueResourceName);
        DruidXADataSource druidXADataSource = new DruidXADataSource();
        druidXADataSource.setUrl(url);
        druidXADataSource.setUsername(userName);
        druidXADataSource.setPassword(password);
        druidXADataSource.setInitialSize(initialSize);
        druidXADataSource.setMinIdle(minIdle);
        druidXADataSource.setMaxActive(maxActive);
        druidXADataSource.setMaxWait(maxWait);
        druidXADataSource.setDriverClassName(driverClassName);
        druidXADataSource.setPoolPreparedStatements(true);
        druidXADataSource.setMaxPoolPreparedStatementPerConnectionSize(33);
        druidXADataSource.setTimeBetweenEvictionRunsMillis(300000);
        druidXADataSource.setUseGlobalDataSourceStat(true);
        druidXADataSource.setFilters("stat,wall,log4j");
        druidXADataSource.setLogAbandoned(true);
        atomikosDataSourceBean.setXaDataSource(druidXADataSource);

        return atomikosDataSourceBean;
    }
}
